package com.threeti.jiayusms.infrastructure.legacy;

import static org.springframework.util.CollectionUtils.isEmpty;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.threeti.jiayusms.domain.model.legacy.PreviewOrder;
import com.threeti.jiayusms.framework.dao.AbstractCustomKeyDao;
import com.threeti.jiayusms.framework.util.PagedListHandler;

@Repository
public class JiaYuDBLegacyDaoImpl extends AbstractCustomKeyDao<String, PreviewOrder> implements JiaYuDBLegacyDao {
  @Autowired
  public JiaYuDBLegacyDaoImpl(SessionFactory sessionFactory) {
    super(sessionFactory);
  }
  @Override
  public PagedListHandler<PreviewOrder> findAllPreviewOrdersByPage(int currentPageIndex, int startingIndex) {
    PagedListHandler<PreviewOrder> pagedListHandler = this.findAllByPage(currentPageIndex, startingIndex);
    List<PreviewOrder> pagedList = pagedListHandler.getPageList();
    if (!isEmpty(pagedList))  return pagedListHandler;

    return PagedListHandler.EMPTY_LIST;
  }

  @Override
  protected DetachedCriteria buildDefaultCriteria() {
    return DetachedCriteria.forClass(PreviewOrder.class);
  }

}
